home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / gdevl256.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-17  |  9.8 KB  |  312 lines

  1. /* Copyright (C) 1989, 1995, 1996 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevl256.c */
  20. /* Ghostscript driver for 256-color VGA modes with Linux and vgalib */
  21. /* This Driver was derived from the BGI-Driver. It was written
  22.    only for my own purpose. I never planned to release it or send
  23.    it to others. So, if something doesn't work, you may send
  24.    me a short note, but don't expect me to correct it. I will
  25.    try my very best, but i have some work to do.
  26.  
  27.    Ludger Kunz           |    ____________|Tel.: 02371/566-230
  28.    FernUniversitaet Hagen|   /| /   / \   |FAX:  02371/52212           
  29.    Lehrgebiet ES         |  / |/   /_  \  |EMAIL:                      
  30.    Frauenstuhlweg 31     | /  |\  /     \ |ludger.kunz@fernuni-hagen.de
  31.    58644 Iserlohn        |/___|_\/_______\|
  32.  */
  33. #include "gx.h"
  34. #include "gxdevice.h"
  35. #include "gserrors.h"
  36.  
  37. #include <errno.h>
  38. #include <vga.h>
  39. #include <vgagl.h>
  40.  
  41. /* The color map for dynamically assignable colors. */
  42. #define first_dc_index 64
  43. private int next_dc_index;
  44.  
  45. #define dc_hash_size 293                    /* prime, >num_dc */
  46. typedef struct {
  47.    ushort rgb, index;
  48. } dc_entry;
  49. private dc_entry dynamic_colors[dc_hash_size + 1];
  50.  
  51. #define XDPI   60      /* to get a more-or-less square aspect ratio */
  52. #define YDPI   60
  53.  
  54. #ifndef A4 /*Letter size*/
  55. #define YSIZE (20.0 * YDPI / 2.5)
  56. #define XSIZE (8.5 / 11)*YSIZE /* 8.5 x 11 inch page, by default */
  57. #else                       /* A4 paper */
  58. #define XSIZE 8.3 /*8.27*/
  59. #define YSIZE 11.7 /*11.69*/
  60. #endif
  61.  
  62.  
  63. /* The device descriptor */
  64. typedef struct gx_device_lvga256 {
  65.    gx_device_common;
  66. } gx_device_lvga256;
  67.  
  68. #define lvga256dev ((gx_device_lvga256 *)dev)
  69.  
  70. private dev_proc_open_device (lvga256_open);
  71. private dev_proc_close_device (lvga256_close);
  72. private dev_proc_map_rgb_color (lvga256_map_rgb_color);
  73. private dev_proc_map_color_rgb (lvga256_map_color_rgb);
  74. private dev_proc_fill_rectangle (lvga256_fill_rectangle);
  75. private dev_proc_tile_rectangle (lvga256_tile_rectangle);
  76. private dev_proc_copy_mono (lvga256_copy_mono);
  77. private dev_proc_copy_color (lvga256_copy_color);
  78. private dev_proc_draw_line (lvga256_draw_line);
  79.  
  80. private gx_device_procs lvga256_procs =
  81. {
  82.    lvga256_open,
  83.    NULL,                   /* get_initial_matrix */
  84.    NULL,                      /* sync_output */
  85.    NULL,                      /* output_page */
  86.    lvga256_close,
  87.    lvga256_map_rgb_color,
  88.    lvga256_map_color_rgb,
  89.    lvga256_fill_rectangle,
  90.    lvga256_tile_rectangle,
  91.    lvga256_copy_mono,
  92.    lvga256_copy_color,
  93.    lvga256_draw_line
  94. };
  95.  
  96. gx_device_lvga256 far_data gs_lvga256_device =
  97. {    std_device_color_body(gx_device_lvga256, &lvga256_procs, "lvga256",
  98.       0, 0,            /* width and height are set in lvga256_open */
  99.       1, 1,            /* density is set in lvga256_open */
  100.       /*dci_color(*/8, 31, 4/*)*/)
  101. };
  102.  
  103. /* Open the LINUX driver for graphics mode */
  104. int
  105. lvga256_open (gx_device * dev)
  106. {
  107.    int vgamode;
  108.    int width, height;
  109.  
  110.    vga_init();
  111.    vgamode = vga_getdefaultmode ();
  112.    if (vgamode == -1)
  113.       vgamode = G320x200x256;
  114.    vga_setmode (vgamode);
  115.    gl_setcontextvga (vgamode);
  116.    width = vga_getxdim();
  117.    height = vga_getydim();
  118.    dev->y_pixels_per_inch = height / 12.0;
  119.    dev->x_pixels_per_inch = dev->y_pixels_per_inch;
  120.    gx_device_set_width_height(dev, width, height);
  121.    {
  122.       int c;
  123.  
  124.       for (c = 0; c < 64; c++) {
  125.          static const byte c2[10] =
  126.          {0, 42, 0, 0, 0, 0, 0, 0, 21, 63};
  127.  
  128.          gl_setpalettecolor (c, c2[(c >> 2) & 9], c2[(c >> 1) & 9], c2[c & 9]);
  129.       }
  130.    }
  131.    /* Initialize the dynamic color table. */
  132.    memset (dynamic_colors, 0, (dc_hash_size + 1) * sizeof (dc_entry));
  133.    next_dc_index = first_dc_index;
  134.  
  135.    return 0;
  136. }
  137.  
  138. /* Close the LINUX driver */
  139. int
  140. lvga256_close (gx_device * dev)
  141. {
  142.    vga_setmode (TEXT);
  143.    return 0;
  144. }
  145.  
  146. /* Map a r-g-b color to a palette index. */
  147. /* The first 64 entries of the color map are set */
  148. /* for compatibility with the older display modes: */
  149. /* these are indexed as 0.0.R0.G0.B0.R1.G1.B1. */
  150. gx_color_index
  151. lvga256_map_rgb_color (gx_device * dev, gx_color_value r, gx_color_value g,
  152.                       gx_color_value b)
  153. {
  154. #define cv_bits(v,n) (v >> (gx_color_value_bits - n))
  155.    ushort r5 = cv_bits (r, 5), g5 = cv_bits (g, 5), b5 = cv_bits (b, 5);
  156.    static const byte cube_bits[32] =
  157.    {0, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  158.     8, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  159.     1, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  160.     9
  161.    };
  162.    uint cx = ((uint) cube_bits[r5] << 2) + ((uint) cube_bits[g5] << 1) +
  163.    (uint) cube_bits[b5];
  164.    ushort rgb;
  165.    register dc_entry _ds *pdc;
  166.  
  167.    /* Check for a color on the cube. */
  168.    if (cx < 64)
  169.       return (gx_color_index) cx;
  170.    /* Not on the cube, check the dynamic color table. */
  171.    rgb = (r5 << 10) + (g5 << 5) + b5;
  172.    for (pdc = &dynamic_colors[rgb % dc_hash_size]; pdc->rgb != 0; pdc++) {
  173.       if (pdc->rgb == rgb)
  174.          return (gx_color_index) (pdc->index);
  175.    }
  176.    if (pdc == &dynamic_colors[dc_hash_size]) {  /* Wraparound */
  177.       for (pdc = &dynamic_colors[0]; pdc->rgb != 0; pdc++) {
  178.          if (pdc->rgb == rgb)
  179.             return (gx_color_index) (pdc->index);
  180.       }
  181.    }
  182.    if (next_dc_index == 256) {              /* No space left, report failure. */
  183.       return gx_no_color_index;
  184.    }
  185.    /* Not on the cube, and not in the dynamic table. */
  186.    /* Put in the dynamic table if space available. */
  187.    {
  188.       int i = next_dc_index++;
  189.  
  190.       pdc->rgb = rgb;
  191.       pdc->index = i;
  192.       gl_setpalettecolor (i,cv_bits (r, 6), cv_bits (g, 6), cv_bits (b, 6));
  193.       return (gx_color_index) i;
  194.    }
  195. }
  196.  
  197. int
  198. lvga256_map_color_rgb (gx_device * dev, gx_color_index color,
  199.                       unsigned short prgb[3])
  200. {
  201. /*   gl_getpalettecolor (color,(int *)&prgb[0],(int *)&prgb[1],(int *)&prgb[2]);*/
  202.    prgb[0]=gx_max_color_value;
  203.    prgb[1]=gx_max_color_value;
  204.    prgb[2]=gx_max_color_value;
  205.    return 0;
  206. }
  207.  
  208. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  209. /* Color = gx_no_color_index means transparent (no effect on the image). */
  210. int
  211. lvga256_copy_mono (gx_device * dev,
  212.                   const byte * base, int sourcex, int raster, gx_bitmap_id id,
  213.                   int x, int y, int w, int h,
  214.                   gx_color_index zero, gx_color_index one)
  215. {
  216.    const byte *ptr_line = base + (sourcex >> 3);
  217.    int left_bit = 0x80 >> (sourcex & 7);
  218.    int dest_y = y, end_x = x + w;
  219.    int invert = 0;
  220.    int color;
  221.  
  222.    fit_copy (dev,base, sourcex, raster, id, x, y, w, h);
  223.    if (zero == gx_no_color_index) {
  224.       if (one == gx_no_color_index)
  225.          return 0;
  226.       color = (int) one;
  227.    }
  228.    else {
  229.       if (one == gx_no_color_index) {
  230.          color = (int) zero;
  231.          invert = -1;
  232.       }
  233.       else {                    /* Pre-clear the rectangle to zero */
  234.          gl_fillbox (x, y, w, h, 0);
  235.          color = (int) one;
  236.       }
  237.    }
  238.    while (h--) {                /* for each line */
  239.       const byte *ptr_source = ptr_line;
  240.       register int dest_x = x;
  241.       register int bit = left_bit;
  242.  
  243.       while (dest_x < end_x) {  /* for each bit in the line */
  244.          if ((*ptr_source ^ invert) & bit) {
  245.             gl_setpixel (dest_x, dest_y, color);
  246.          }
  247.          dest_x++;
  248.          if ((bit >>= 1) == 0)
  249.             bit = 0x80, ptr_source++;
  250.       }
  251.       dest_y++;
  252.       ptr_line += raster;
  253.    }
  254.    return 0;
  255. }
  256.  
  257. /* Copy a color pixel map.  This is just like a bitmap, except that */
  258. /* each pixel takes 4 bits instead of 1 when device driver has color. */
  259. int
  260. lvga256_copy_color (gx_device * dev,
  261.                    const byte * base, int sourcex, int raster, gx_bitmap_id id,
  262.                    int x, int y, int w, int h)
  263. {
  264.    fit_copy (dev,base, sourcex, raster, id, x, y, w, h);
  265.    if (gx_device_has_color (dev)) { /* color device, four bits per pixel */
  266.       const byte *line = base + sourcex;
  267.  
  268.       gl_putbox (x, y, w, h, line);
  269.    }
  270.    else {                       /* monochrome device: one bit per pixel */
  271.       /* bit map is the same as lvga256_copy_mono: one bit per pixel */
  272.       lvga256_copy_mono (dev, base, sourcex, raster, id, x, y, w, h,
  273.                         (gx_color_index) 0, (gx_color_index) 255);
  274.    }
  275.    return 0;
  276. }
  277.  
  278. /* Fill a rectangle. */
  279. int
  280. lvga256_fill_rectangle (gx_device * dev, int x, int y, int w, int h,
  281.                        gx_color_index color)
  282. {
  283.    fit_fill (dev,x,y,w,h);
  284.    gl_fillbox (x, y, w, h, color);
  285.    return 0;
  286. }
  287.  
  288. /* Tile a rectangle.  If neither color is transparent, */
  289. /* pre-clear the rectangle to color0 and just tile with color1. */
  290. /* This is faster because of how lvga256_copy_mono is implemented. */
  291. /* Note that this also does the right thing for colored tiles. */
  292. int
  293. lvga256_tile_rectangle (gx_device * dev, const gx_tile_bitmap * tile,
  294.                        int x, int y, int w, int h, gx_color_index czero, gx_color_index cone,
  295.                        int px, int py)
  296. {
  297.    if (czero != gx_no_color_index && cone != gx_no_color_index) {
  298.       lvga256_fill_rectangle (dev, x, y, w, h, czero);
  299.       czero = gx_no_color_index;
  300.    }
  301.    return gx_default_tile_rectangle (dev, tile, x, y, w, h, czero, cone, px, py);
  302. }
  303.  
  304. /* Draw a line */
  305. int
  306. lvga256_draw_line (gx_device * dev, int x0, int y0, int x1, int y1,
  307.                   gx_color_index color)
  308. {
  309.    gl_line (x0, y0, x1, y1, color);
  310.    return 0;
  311. }
  312.